{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# DeepPurpose Deep Dive\n",
    "## Tutorial 1: Training a Drug-Target Interaction Model from Scratch\n",
    "#### [@KexinHuang5](https://twitter.com/KexinHuang5)\n",
    "\n",
    "In this tutorial, we take a deep dive into DeepPurpose and show how it builds a drug-target interaction model from scratch. \n",
    "\n",
    "Agenda:\n",
    "\n",
    "- Part I: Overview of DeepPurpose and Data\n",
    "- Part II: Drug Target Interaction Prediction\n",
    "    - DeepPurpose Framework\n",
    "    - Applications to Drug Repurposing and Virtual Screening\n",
    "    - Pretrained Models\n",
    "    - Hyperparameter Tuning\n",
    "    - Model Robustness Evaluation\n",
    "\n",
    "Let's start!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "RDKit WARNING: [15:58:25] Enabling RDKit 2019.09.3 jupyter extensions\n"
     ]
    }
   ],
   "source": [
    "from DeepPurpose import utils, models, dataset\n",
    "import warnings\n",
    "warnings.filterwarnings(\"ignore\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Part I: Overview of DeepPurpose and Data\n",
    "\n",
    "Drug-target interaction measures the binding of drug molecules to the protein targets. Accurate identification of DTI is fundamental for drug discovery and supports many downstream tasks. Among others, drug screening and repurposing are two main applications based on DTI. Drug screening helps identify ligand candidates that can bind to the protein of interest, whereas drug repurposing finds new therapeutic purposes for existing drugs. Both tasks could alleviate the costly, time-consuming, and labor-intensive process of synthesis and analysis, which is extremely important, especially in the cases of hunting effective and safe treatments for COVID-19.\n",
    "\n",
    "DeepPurpose is a pytorch-based deep learning framework that is initiated to provide a simple but powerful toolkit for drug-target interaction prediction and its related applications. We see many exciting recent works in this direction, but to leverage these models, it takes lots of efforts due to the esoteric instructions and interface. DeepPurpose is designed to make things as simple as possible using a unified framework.\n",
    "\n",
    "DeepPurpose uses an encoder-decoder framework. Drug repurposing and screening are two applications after we obtain DTI models. The input to the model is a drug target pair, where drug uses the simplified molecular-input line-entry system (SMILES) string and target uses the amino acid sequence. The output is a score indicating the binding activity of the drug target pair. Now, we begin talking about the data format expected.\n",
    "\n",
    "\n",
    "(**Data**) DeepPurpose takes into an array of drug's SMILES strings (**d**), an array of target protein's amino acid sequence (**t**), and an array of label (**y**), which can either be binary 0/1 indicating interaction outcome or a real number indicating affinity value. The input drug and target arrays should be paired, i.e. **y**\\[0\\] is the score for **d**\\[0\\] and **t**\\[0\\].\n",
    "\n",
    "Besides transforming into numpy arrays through some data wrangling on your own, DeepPurpose also provides two ways to help data preparation. \n",
    "\n",
    "The first way is to read from local files. For example, to load drug target pairs, we expect a file.txt where each line is a drug SMILES string, followed by a protein sequence, and an affinity score or 0/1 label:\n",
    "\n",
    "```CC1=C...C4)N MKK...LIDL 7.365``` \\\n",
    "```CC1=C...C4)N QQP...EGKH 4.999```\n",
    "\n",
    "Then, we use ```dataset.read_file_training_dataset_drug_target_pairs``` to load it."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Drug 1: CC1=C2C=C(C=CC2=NN1)C3=CC(=CN=C3)OCC(CC4=CC=CC=C4)N\n",
      "Target 1: MKKFFDSRREQGGSGLGSGSSGGGGSTSGLGSGYIGRVFGIGRQQVTVDEVLAEGGFAIVFLVRTSNGMKCALKRMFVNNEHDLQVCKREIQIMRDLSGHKNIVGYIDSSINNVSSGDVWEVLILMDFCRGGQVVNLMNQRLQTGFTENEVLQIFCDTCEAVARLHQCKTPIIHRDLKVENILLHDRGHYVLCDFGSATNKFQNPQTEGVNAVEDEIKKYTTLSYRAPEMVNLYSGKIITTKADIWALGCLLYKLCYFTLPFGESQVAICDGNFTIPDNSRYSQDMHCLIRYMLEPDPDKRPDIYQVSYFSFKLLKKECPIPNVQNSPIPAKLPEPVKASEAAAKKTQPKARLTDPIPTTETSIAPRQRPKAGQTQPNPGILPIQPALTPRKRATVQPPPQAAGSSNQPGLLASVPQPKPQAPPSQPLPQTQAKQPQAPPTPQQTPSTQAQGLPAQAQATPQHQQQLFLKQQQQQQQPPPAQQQPAGTFYQQQQAQTQQFQAVHPATQKPAIAQFPVVSQGGSQQQLMQNFYQQQQQQQQQQQQQQLATALHQQQLMTQQAALQQKPTMAAGQQPQPQPAAAPQPAPAQEPAIQAPVRQQPKVQTTPPPAVQGQKVGSLTPPSSPKTQRAGHRRILSDVTHSAVFGVPASKSTQLLQAAAAEASLNKSKSATTTPSGSPRTSQQNVYNPSEGSTWNPFDDDNFSKLTAEELLNKDFAKLGEGKHPEKLGGSAESLIPGFQSTQGDAFATTSFSAGTAEKRKGGQTVDSGLPLLSVSDPFIPLQVPDAPEKLIEGLKSPDTSLLLPDLLPMTDPFGSTSDAVIEKADVAVESLIPGLEPPVPQRLPSQTESVTSNRTDSLTGEDSLLDCSLLSNPTTDLLEEFAPTAISAPVHKAAEDSNLISGFDVPEGSDKVAEDEFDPIPVLITKNPQGGHSRNSSGSSESSLPNLARSLLLVDQLIDL\n",
      "Score 1: 7.365\n"
     ]
    }
   ],
   "source": [
    "X_drugs, X_targets, y = dataset.read_file_training_dataset_drug_target_pairs('./toy_data/dti.txt')\n",
    "print('Drug 1: ' + X_drugs[0])\n",
    "print('Target 1: ' + X_targets[0])\n",
    "print('Score 1: ' + str(y[0]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Many method researchers want to test on benchmark datasets such as KIBA/DAVIS/BindingDB, DeepPurpose also provides data loaders to ease preprocessing. For example, we want to load the DAVIS dataset, we can use ```dataset.load_process_DAVIS```. It will download, preprocess to the designated data format. It supports label log-scale transformation for easier regression and also allows label binarization given a customized threshold."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Beginning Processing...\n",
      "Beginning to extract zip file...\n",
      "Default set to logspace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 7.3655227298392685\n"
     ]
    }
   ],
   "source": [
    "X_drugs, X_targets, y = dataset.load_process_DAVIS(path = './data', binary = False, convert_to_log = True, threshold = 30)\n",
    "print('Drug 1: ' + X_drugs[0])\n",
    "print('Target 1: ' + X_targets[0])\n",
    "print('Score 1: ' + str(y[0]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For more detailed examples and tutorials of data loading, checkout this [tutorial](./DEMO/load_data_tutorial.ipynb).\n",
    "\n",
    "## Part II: Drug Target Interaction Prediction Framework\n",
    "\n",
    "DeepPurpose provides a simple framework to conduct DTI research using 8 encoders for drugs and 7 for proteins. It basically consists of the following steps, where each step corresponds to one line of code:\n",
    "\n",
    "- Encoder specification\n",
    "- Data encoding and split\n",
    "- Model configuration generation\n",
    "- Model initialization\n",
    "- Model Training\n",
    "- Model Prediction and Repuposing/Screening\n",
    "- Model Saving and Loading\n",
    "\n",
    "Let's start with data encoding! \n",
    "\n",
    "(**Encoder specification**) After we obtain the required data format from Part I, we need to prepare them for the encoders. Hence, we first specify the encoder to use for drug and protein. Here we try MPNN for drug and CNN for target.\n",
    "\n",
    "If you find MPNN and CNN are too large for the CPUs, you can try smaller encoders by uncommenting the last line:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "drug_encoding, target_encoding = 'MPNN', 'CNN'\n",
    "#drug_encoding, target_encoding = 'Morgan', 'Conjoint_triad'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note that you can switch encoder just by changing the encoding name above. The full list of encoders are listed [here](https://github.com/kexinhuang12345/DeepPurpose#encodings). Here, we are using the message passing neural network encoder for drug and convolutional neural network encoder for protein.\n",
    "\n",
    "(**Data encoding and split**) Now, we encode the data into the specified format, using ```utils.data_process``` function. It specifies train/validation/test split fractions, and random seed to ensure same data splits for reproducibility. This function also support data splitting methods such as ```cold_drug``` and ```cold_protein```, which splits on drug/proteins for model robustness evaluation to test on unseen drug/proteins.\n",
    "\n",
    "The function outputs train, val, test pandas dataframes."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "in total: 30056 drug-target pairs\n",
      "encoding drug...\n",
      "unique drugs: 68\n",
      "drug encoding finished...\n",
      "encoding protein...\n",
      "unique target sequence: 379\n",
      "protein encoding finished...\n",
      "splitting dataset...\n",
      "Done.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>SMILES</th>\n",
       "      <th>Target Sequence</th>\n",
       "      <th>Label</th>\n",
       "      <th>drug_encoding</th>\n",
       "      <th>target_encoding</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>CC1=C2C=C(C=CC2=NN1)C3=CC(=CN=C3)OCC(CC4=CC=CC...</td>\n",
       "      <td>PFWKILNPLLERGTYYYFMGQQPGKVLGDQRRPSLPALHFIKGAGK...</td>\n",
       "      <td>4.999996</td>\n",
       "      <td>[[[tensor(1.), tensor(0.), tensor(0.), tensor(...</td>\n",
       "      <td>[P, F, W, K, I, L, N, P, L, L, E, R, G, T, Y, ...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                              SMILES  \\\n",
       "0  CC1=C2C=C(C=CC2=NN1)C3=CC(=CN=C3)OCC(CC4=CC=CC...   \n",
       "\n",
       "                                     Target Sequence     Label  \\\n",
       "0  PFWKILNPLLERGTYYYFMGQQPGKVLGDQRRPSLPALHFIKGAGK...  4.999996   \n",
       "\n",
       "                                       drug_encoding  \\\n",
       "0  [[[tensor(1.), tensor(0.), tensor(0.), tensor(...   \n",
       "\n",
       "                                     target_encoding  \n",
       "0  [P, F, W, K, I, L, N, P, L, L, E, R, G, T, Y, ...  "
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train, val, test = utils.data_process(X_drugs, X_targets, y, \n",
    "                                drug_encoding, target_encoding, \n",
    "                                split_method='random',frac=[0.7,0.1,0.2],\n",
    "                                random_seed = 1)\n",
    "train.head(1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "(**Model configuration generation**) Now, we initialize a model with its configuration. You can modify almost any hyper-parameters (e.g., learning rate, epoch, batch size), model parameters (e.g. hidden dimensions, filter size) and etc in this function. The supported configurations are listed here in this [link](https://github.com/kexinhuang12345/DeepPurpose/blob/e169e2f550694145077bb2af95a4031abe400a77/DeepPurpose/utils.py#L486).\n",
    "\n",
    "For the sake of example, we specify the epoch size to be 3, and set the model parameters to be small so that you can run on both CPUs & GPUs quickly and can proceed to the next steps. For a reference parameters, checkout the notebooks in the DEMO folder."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "config = utils.generate_config(drug_encoding = drug_encoding, \n",
    "                         target_encoding = target_encoding, \n",
    "                         cls_hidden_dims = [1024,1024,512], \n",
    "                         train_epoch = 5, \n",
    "                         LR = 0.001, \n",
    "                         batch_size = 128,\n",
    "                         hidden_dim_drug = 128,\n",
    "                         mpnn_hidden_size = 128,\n",
    "                         mpnn_depth = 3, \n",
    "                         cnn_target_filters = [32,64,96],\n",
    "                         cnn_target_kernels = [4,8,12]\n",
    "                        )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "(**Model initialization**) Next, we initialize a model using the above configuration."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<DeepPurpose.models.DBTA at 0x7fe8e6689f10>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model = models.model_initialize(**config)\n",
    "model"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "(**Model Training**) Next, it is ready to train, using the ```model.train``` function! If you do not have test set, you can just use ```model.train(train, val)```. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Let's use 1 GPU!\n",
      "--- Data Preparation ---\n",
      "--- Go for Training ---\n",
      "Training at Epoch 1 iteration 0 with loss 30.3964. Total time 0.0 hours\n",
      "Training at Epoch 1 iteration 100 with loss 0.59583. Total time 0.01277 hours\n",
      "Validation at Epoch 1 , MSE: 0.79965 , Pearson Correlation: 0.34392 with p-value: 3.23317 , Concordance Index: 0.67805\n",
      "Training at Epoch 2 iteration 0 with loss 0.62158. Total time 0.02305 hours\n",
      "Training at Epoch 2 iteration 100 with loss 0.83107. Total time 0.03583 hours\n",
      "Validation at Epoch 2 , MSE: 0.65400 , Pearson Correlation: 0.46281 with p-value: 1.79746 , Concordance Index: 0.74763\n",
      "Training at Epoch 3 iteration 0 with loss 0.68138. Total time 0.04611 hours\n",
      "Training at Epoch 3 iteration 100 with loss 0.73255. Total time 0.05888 hours\n",
      "Validation at Epoch 3 , MSE: 0.57770 , Pearson Correlation: 0.52944 with p-value: 7.37415 , Concordance Index: 0.77889\n",
      "Training at Epoch 4 iteration 0 with loss 0.46632. Total time 0.06916 hours\n",
      "Training at Epoch 4 iteration 100 with loss 0.38746. Total time 0.08194 hours\n",
      "Validation at Epoch 4 , MSE: 0.55305 , Pearson Correlation: 0.56039 with p-value: 3.36890 , Concordance Index: 0.78732\n",
      "Training at Epoch 5 iteration 0 with loss 0.54152. Total time 0.09194 hours\n",
      "Training at Epoch 5 iteration 100 with loss 0.59380. Total time 0.10472 hours\n",
      "Validation at Epoch 5 , MSE: 0.61464 , Pearson Correlation: 0.56462 with p-value: 9.73960 , Concordance Index: 0.79126\n",
      "--- Go for Testing ---\n",
      "Testing MSE: 0.5567689292029449 , Pearson Correlation: 0.5585790614300189 with p-value: 0.0 , Concordance Index: 0.783525396597284\n",
      "--- Training Finished ---\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYMAAAELCAYAAAA7h+qnAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjMsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+AADFEAAAdV0lEQVR4nO3de5wdZZ3n8c+3O4HYiSshRIhI0oqMio5y6XEBXUaNOyIzA+jArmy/FEZmW4O+hBF3Rs24xBmiw2sU1PGytnKbSYMil5Hh5iiiLqygHUUEIwKSRCSEBrn3qJPkt388dehTJ6e7q0/Orbu+79erXufUU3Xq+Z3q0+d3nqfqqVJEYGZm5dbT6QDMzKzznAzMzMzJwMzMnAzMzAwnAzMzA+Z1OoBG7bXXXtHf39/pMMzMZpX169c/HBFLa8tnbTLo7+9ndHS002GYmc0qkjbVK3c3kZmZORmYmZmTgZmZ4WRgZmY4GZiZGSVLBiMj0N8PPT3pcWSk0xGZmXWHWXtq6UyNjMDQEIyPp/lNm9I8wOBg5+IyM+sGpWkZrF49kQgqxsdTuZlZ2bU1GUhaIOn7kn4s6U5JH8nKXyDpVkl3S/qKpN2aXffmzTMrNzMrk3a3DH4LvD4iXgkcBBwl6TDgbODciDgAeBQ4pdkVL18+s3IzszJpazKI5Klsdn42BfB64LKs/CLguGbXvXYt9PXly/r6UrmZWdm1/ZiBpF5JtwEPAd8A7gUei4ht2Sr3A/tO8tohSaOSRsfGxmZU7+AgDA/DHnuk+f32S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7iWzOiojfSvpRNvuL6i6Z7NLdZwMXRMQ7qspvJV2/6hTS1UkrFgOHRrogW3Udf1H12l7geuDO7PWnRcSYpHuzVW6LiHumCfss4BfAmyK7vr2k72UxnUFKSBV7AUdGxN3Zej8kJYT/Bnx0mnrMctwysLI6nHR3rhFJ8yoT6VjDz4Aja9a/pTYRAGTdNDdKeoR0K9H/AH4PeHHtutPJbgZzCPCVmLjRCRFxH3Az8Ic1L7m7kgiy9R4itUyWYzZDbhlYWVXuuPXNSZY/WjO/U7dOdrrqtaRbE56SrbOddHbQggZiWky6mme9LqQHgRU1ZfWua//bBuu2knMysLJ6JHs8mdStU+vJmvl6l/f9M1Jr4C2RblQCPHM3q8caiOnRrJ596izbh4mYzZrOycDmut9mj8+qKf9/pC/8F0XERQ1uu4/UEngmUUh6Pamb5r6q9SaLIScinpa0HjhB0pqI2J5tcwXpblj/2GCcZtNyMrC5bivpF/VbJd1OujfvfRHxiKT/BXxW0lLgOtLNxvcl9c1/OyIunmbb1wOnAxdKuoB0rODDwK9q1vtp9vhuSReRjivcPsk4hQ+Tzia6WtLngEWkM5geBz4xg/dtNiM+gGxzWkTsAP6C1B//TdLtT/80W/YF4BjSwd5/JiWEj5B+JN1WYNtfB95Luk/y1cA7gLeT7kpVvd6PgTVZvTdlMTxvkm1eTxrDsAdwKfB/gA3AayKiDPcItg7xnc7MzMwtAzMzczIwMzOcDMzMDCcDMzPDycDMzHAyMDMznAzMzAwnAzMzA/4/TKhrh8+tbDcAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "model.train(train, val, test)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We see that the model will automatically generate and plot the training process, along with the validation result and test result.\n",
    "\n",
    "(**Model Prediction and Repuposing/Screening**) Next, we see how we can predict affinity scores on new data. Suppose the new data is a drug-target pair below."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "in total: 1 drug-target pairs\n",
      "encoding drug...\n",
      "unique drugs: 1\n",
      "drug encoding finished...\n",
      "encoding protein...\n",
      "unique target sequence: 1\n",
      "protein encoding finished...\n",
      "splitting dataset...\n",
      "do not do train/test split on the data for already splitted data\n",
      "predicting...\n",
      "The predicted score is [5.243870735168457]\n"
     ]
    }
   ],
   "source": [
    "X_drug = ['CC1=C2C=C(C=CC2=NN1)C3=CC(=CN=C3)OCC(CC4=CC=CC=C4)N']\n",
    "X_target = ['MKKFFDSRREQGGSGLGSGSSGGGGSTSGLGSGYIGRVFGIGRQQVTVDEVLAEGGFAIVFLVRTSNGMKCALKRMFVNNEHDLQVCKREIQIMRDLSGHKNIVGYIDSSINNVSSGDVWEVLILMDFCRGGQVVNLMNQRLQTGFTENEVLQIFCDTCEAVARLHQCKTPIIHRDLKVENILLHDRGHYVLCDFGSATNKFQNPQTEGVNAVEDEIKKYTTLSYRAPEMVNLYSGKIITTKADIWALGCLLYKLCYFTLPFGESQVAICDGNFTIPDNSRYSQDMHCLIRYMLEPDPDKRPDIYQVSYFSFKLLKKECPIPNVQNSPIPAKLPEPVKASEAAAKKTQPKARLTDPIPTTETSIAPRQRPKAGQTQPNPGILPIQPALTPRKRATVQPPPQAAGSSNQPGLLASVPQPKPQAPPSQPLPQTQAKQPQAPPTPQQTPSTQAQGLPAQAQATPQHQQQLFLKQQQQQQQPPPAQQQPAGTFYQQQQAQTQQFQAVHPATQKPAIAQFPVVSQGGSQQQLMQNFYQQQQQQQQQQQQQQLATALHQQQLMTQQAALQQKPTMAAGQQPQPQPAAAPQPAPAQEPAIQAPVRQQPKVQTTPPPAVQGQKVGSLTPPSSPKTQRAGHRRILSDVTHSAVFGVPASKSTQLLQAAAAEASLNKSKSATTTPSGSPRTSQQNVYNPSEGSTWNPFDDDNFSKLTAEELLNKDFAKLGEGKHPEKLGGSAESLIPGFQSTQGDAFATTSFSAGTAEKRKGGQTVDSGLPLLSVSDPFIPLQVPDAPEKLIEGLKSPDTSLLLPDLLPMTDPFGSTSDAVIEKADVAVESLIPGLEPPVPQRLPSQTESVTSNRTDSLTGEDSLLDCSLLSNPTTDLLEEFAPTAISAPVHKAAEDSNLISGFDVPEGSDKVAEDEFDPIPVLITKNPQGGHSRNSSGSSESSLPNLARSLLLVDQLIDL']\n",
    "y = [7.365]\n",
    "X_pred = utils.data_process(X_drug, X_target, y, \n",
    "                                drug_encoding, target_encoding, \n",
    "                                split_method='no_split')\n",
    "y_pred = model.predict(X_pred)\n",
    "print('The predicted score is ' + str(y_pred))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can also do repurposing and screening using the trained model. Basically, for repurposing a set of existing drugs (**r**) for a single new target (*t*), we run the above prediction function after pairing each repurposing drug with the target. Similarly, for screening, we instead have a set of drug-target pairs (**d**, **t**). We wrap the operation into a ```models.repurpose``` and ```models.virtual_screening``` methods.\n",
    "\n",
    "For example, suppose we want to do repurposing from a set of antiviral drugs for a COVID-19 target 3CL protease. The corresponding data can be retrieved using ```dataset``` functions.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Target Name: SARS-CoV2 3CL Protease\n",
      "Amino Acid Sequence: SGFRKMAFPSGKVEGCMVQVTCGTTTLNGLWLDDVVYCPRHVICTSEDMLNPNYEDLLIRKSNHNFLVQAGNVQLRVIGHSMQNCVLKLKVDTANPKTPKYKFVRIQPGQTFSVLACYNGSPSGVYQCAMRPNFTIKGSFLNGSCGSVGFNIDYDCVSFCYMHHMELPTGVHAGTDLEGNFYGPFVDRQTAQAAGTDTTITVNVLAWLYAAVINGDRWFLNRFTTTLNDFNLVAMKYNYEPLTQDHVDILGPLSAQTGIAVLDMCASLKELLQNGMNGRTILGSALLEDEFTPFDVVRQCSGVTFQ\n"
     ]
    }
   ],
   "source": [
    "t, t_name = dataset.load_SARS_CoV2_Protease_3CL()\n",
    "print('Target Name: ' + t_name)\n",
    "print('Amino Acid Sequence: '+ t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Repurposing Drug 1 Name: Abacavir\n",
      "Repurposing Drug 1 SMILES: C1CC1NC2=C3C(=NC(=N2)N)N(C=N3)C4CC(C=C4)CO\n",
      "Repurposing Drug 1 Pubchem CID: 441300\n"
     ]
    }
   ],
   "source": [
    "r, r_name, r_pubchem_cid = dataset.load_antiviral_drugs()\n",
    "print('Repurposing Drug 1 Name: ' + r_name[0])\n",
    "print('Repurposing Drug 1 SMILES: ' + r[0])\n",
    "print('Repurposing Drug 1 Pubchem CID: ' + str(r_pubchem_cid[0]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now, we can call the ```repurpose``` function. After feeding the necessary inputs, it will print a list of repurposed drugs ranked on its affinity to the target protein. The ```convert_y``` parameter should be set to be ```False``` when the ranking is ascending (i.e. lower value -> higher affinity) due to the log transformation, vice versus."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "repurposing...\n",
      "in total: 82 drug-target pairs\n",
      "encoding drug...\n",
      "unique drugs: 81\n",
      "drug encoding finished...\n",
      "encoding protein...\n",
      "unique target sequence: 1\n",
      "protein encoding finished...\n",
      "Done.\n",
      "predicting...\n",
      "---------------\n",
      "Drug Repurposing Result for SARS-CoV2 3CL Protease\n",
      "+------+----------------------+------------------------+---------------+\n",
      "| Rank |      Drug Name       |      Target Name       | Binding Score |\n",
      "+------+----------------------+------------------------+---------------+\n",
      "|  1   |      Foscarnet       | SARS-CoV2 3CL Protease |      0.14     |\n",
      "|  2   |      Efavirenz       | SARS-CoV2 3CL Protease |     20.86     |\n",
      "|  3   |     Rimantadine      | SARS-CoV2 3CL Protease |     34.61     |\n",
      "|  4   |      Sofosbuvir      | SARS-CoV2 3CL Protease |     81.58     |\n",
      "|  5   |     Glecaprevir      | SARS-CoV2 3CL Protease |     83.02     |\n",
      "|  6   |      Remdesivir      | SARS-CoV2 3CL Protease |     89.09     |\n",
      "|  7   |     Grazoprevir      | SARS-CoV2 3CL Protease |     229.87    |\n",
      "|  8   |      Amantadine      | SARS-CoV2 3CL Protease |     344.43    |\n",
      "|  9   |      Simeprevir      | SARS-CoV2 3CL Protease |     434.88    |\n",
      "|  10  |     Tromantadine     | SARS-CoV2 3CL Protease |     624.48    |\n",
      "checkout ./result/repurposing.txt for the whole list\n"
     ]
    }
   ],
   "source": [
    "y_pred = models.repurpose(X_repurpose = r, target = t, model = model, drug_names = r_name, target_name = t_name, \n",
    "                          result_folder = \"./result/\", convert_y = True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now, let's move on to showcase how to do virtual screening. We first load a sample of data from BindingDB dataset. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Downloading...\n"
     ]
    }
   ],
   "source": [
    "t, d = dataset.load_IC50_1000_Samples()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can then use the ```virtual_screening``` function to generate a list of drug-target pairs that have high binding affinities. If no drug/target names are provided, the index of the drug/target list is used instead."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "virtual screening...\n",
      "in total: 100 drug-target pairs\n",
      "encoding drug...\n",
      "unique drugs: 100\n",
      "drug encoding finished...\n",
      "encoding protein...\n",
      "unique target sequence: 93\n",
      "protein encoding finished...\n",
      "Done.\n",
      "predicting...\n",
      "---------------\n",
      "Virtual Screening Result\n",
      "+------+-----------+-------------+---------------+\n",
      "| Rank | Drug Name | Target Name | Binding Score |\n",
      "+------+-----------+-------------+---------------+\n",
      "|  1   |  Drug 83  |  Target 83  |      8.87     |\n",
      "|  2   |  Drug 53  |  Target 53  |      7.71     |\n",
      "|  3   |  Drug 60  |  Target 60  |      7.01     |\n",
      "|  4   |  Drug 41  |  Target 41  |      6.73     |\n",
      "|  5   |  Drug 93  |  Target 93  |      6.59     |\n",
      "|  6   |  Drug 32  |  Target 32  |      6.49     |\n",
      "|  7   |   Drug 0  |   Target 0  |      6.43     |\n",
      "|  8   |  Drug 64  |  Target 64  |      6.27     |\n",
      "|  9   |  Drug 23  |  Target 23  |      6.26     |\n",
      "|  10  |  Drug 21  |  Target 21  |      6.23     |\n",
      "checkout ./result/virtual_screening.txt for the whole list\n",
      "\n"
     ]
    }
   ],
   "source": [
    "y_pred = models.virtual_screening(d, t, model)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Saving and loading models are also really easy. The loading function also automatically detects if the model is trained on multiple GPUs. To save a model:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "model.save_model('./tutorial_model')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To load a saved/pretrained model:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<DeepPurpose.models.DBTA at 0x7fe8e1472310>"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model = models.model_pretrained(path_dir = './tutorial_model')\n",
    "model"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We have also provided a list of pretrained model, you can find all available ones under the [list](https://github.com/kexinhuang12345/DeepPurpose#pretrained-models). For example, to load a MPNN+CNN model pretrained on BindingDB Kd dataset:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Beginning Downloading MPNN_CNN_BindingDB Model...\n",
      "Downloading finished... Beginning to extract zip file...\n",
      "pretrained model Successfully Downloaded...\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<DeepPurpose.models.DBTA at 0x7fe8e625ae90>"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model = models.model_pretrained(model = 'MPNN_CNN_BindingDB')\n",
    "model"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We also provided many more functionalities for DTI research purposes. \n",
    "\n",
    "For example, this [demo](https://github.com/kexinhuang12345/DeepPurpose/blob/master/DEMO/Drug_Property_Pred-Ax-Hyperparam-Tune.ipynb) shows how to use Ax platform to do some latest hyperparameter tuning methods such as Bayesian Optimization on DeepPurpose.\n",
    "\n",
    "Model robustness is very important for DTI task. One way to measure is to see how the model can predict drug or protein that do not exist in the training set, i.e., cold drug/target setting. You can achieve this by modifying the ```split_method``` parameter in the ```data_process``` function: "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Beginning Processing...\n",
      "Beginning to extract zip file...\n",
      "Default set to logspace (nM -> p) for easier regression\n",
      "Done!\n",
      "in total: 30056 drug-target pairs\n",
      "encoding drug...\n",
      "unique drugs: 68\n",
      "drug encoding finished...\n",
      "encoding protein...\n",
      "unique target sequence: 379\n",
      "protein encoding finished...\n",
      "splitting dataset...\n",
      "Done.\n"
     ]
    }
   ],
   "source": [
    "X_drugs, X_targets, y = dataset.load_process_DAVIS(path = './data', binary = False, convert_to_log = True, threshold = 30)\n",
    "train, val, test = utils.data_process(X_drugs, X_targets, y, \n",
    "                                drug_encoding, target_encoding, \n",
    "                                split_method='cold_drug',frac=[0.7,0.1,0.2],\n",
    "                                random_seed = 1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "That wraps up our tutorials on the main functionalities of DeepPurpose's Drug Target Interaction Prediction framework! \n",
    "\n",
    "Do checkout the upcoming tutorials:\n",
    "\n",
    "Tutorial 2: Drug Property Prediction using DeepPurpose\n",
    "\n",
    "Tutorial 3: Repurposing and Virtual Screening Using One Line of Code\n",
    "\n",
    "**Star & watch & contribute to DeepPurpose's [github repository](https://github.com/kexinhuang12345/DeepPurpose)!**\n",
    "\n",
    "Feedbacks would also be appreciated and you can send me an email (kexinhuang@hsph.harvard.edu)!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
